import type { FunctionModel } from "@/model/function_model/FunctionModel";
import { createRouter, createWebHashHistory, type RouteMeta, type RouteRecordRaw } from "vue-router";


//用于获取模块meta信息.
const pageInfos = import.meta.glob('../pages/**/Page.ts',{
    eager:true,
    import:'default'
})
//存储所有的导入函数.
const pageModules = import.meta.glob('../pages/**/Index.vue')

/**
 * 页面的vue默认命名为Index.vue
 */
const defaultPageFileName:string = "Index.vue";
const defaultPageMetaFileName:string = "Page.ts"

/**
 * 路由规则:
 * src/pages/a/b/c/Index.vue
 * path: /a/b/c
 * name: a_b_c
 * 
 * /a/Index.vue
 * -->a
 */

/**
 * filter(Boolean)是什么意思?
 */

/**
 * vite是由rollup打包.
 * import里不能写变量,只能是自变量.
 */

/**
 * 将key,value映射为我们需要的路由对象.
 * key为文件路径
 */
const autoRoutes:RouteRecordRaw[] = Object.entries(pageModules).map(([key,value])=>{
    const path = key.replace('../pages','').replace(`/${defaultPageFileName}`,'');
    let name = path.split('/').filter(Boolean).join('_');
    let infoPath = key.replace(`${defaultPageFileName}`,`${defaultPageMetaFileName}`);
    let meta:RouteMeta|null = pageInfos[infoPath] as RouteMeta;
    let route:RouteRecordRaw = {
        path:path,
        name:name,
        component:value,
        meta:meta
    }
    return route;
});


/**
 * 增加一个默认路由为登录页面.
 */
let  routeHome:RouteRecordRaw = {
    path:'/',
    name:"login",
    component: () => import("@/pages/01-login/Index.vue"),
}
autoRoutes.splice(0,0,routeHome);



// debugger
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: autoRoutes,
});

export default router;
